package com.besall.allbase.view.activity.chipstoollevel4.protocol.FitnessParams;

import static com.besall.allbase.bluetooth.service.Protocol.ProtocolConstants.KEY_LATEST_HEALTH_VALUE;
import static com.besall.allbase.bluetooth.service.Protocol.ProtocolConstants.KEY_PROBE_INV_VALUE;

import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.DialogFragment;

import com.bes.bessdk.utils.SPHelper;
import  bb.lanxing.R;
import com.weigan.loopview.LoopView;
import com.weigan.loopview.OnItemSelectedListener;

import java.util.ArrayList;

public class LatestHealthData extends DialogFragment implements View.OnClickListener {
    private Context mContext;
    private ConfigCallBack mConfigCallback;
    private int interval = 0;

    String[] options = {"HR","SPO2","TEMPERATURE","BP and so on"};

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE);
        mContext = inflater.getContext();
        View view = inflater.inflate(R.layout.config_probeinv, container, false);
        initView(view);
        return view;
    }

    public void initView(View view){
        LoopView loopView = (LoopView) view.findViewById(R.id.loopView);
        ArrayList<String> hourList = new ArrayList<>();
        for (int i = 0; i < 4; i++) {
            hourList.add(options[i]);
        }
        loopView.setListener(new OnItemSelectedListener() {
            @Override
            public void onItemSelected(int index) {
                interval = index;
            }
        });
        loopView.setItems(hourList);

        loopView.setInitPosition(0);


        view.findViewById(R.id.ok).setOnClickListener(mOkListener);
        view.findViewById(R.id.cancel).setOnClickListener(mCancelListener);

    }

    public boolean saveConfig(){
        SPHelper.putPreference(getActivity(), KEY_LATEST_HEALTH_VALUE, interval);
        return true;
    }
    public void setConfigCallback(ConfigCallBack configCallback) {
        mConfigCallback = configCallback;
    }


    private final View.OnClickListener mOkListener =new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (saveConfig()){
                dismissAllowingStateLoss();
                if (mConfigCallback != null){
                    mConfigCallback.onConfigOk();
                }
            }
        }
    };

    private final View.OnClickListener mCancelListener =new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            dismissAllowingStateLoss();
            if (mConfigCallback != null){
                mConfigCallback.onConfigCancel();
            }
        }
    };
    @Override
    public void onClick(View v) {
        dismissAllowingStateLoss();
        if (mConfigCallback != null){
            mConfigCallback.modeClick(v.getId());
            Log.i("TAG", "onClick: ============" + v.getId());
        }
    }


    public interface ConfigCallBack {

        void modeClick(int id);

        void onConfigOk();

        void onConfigCancel();
    }



}
